//
//  UniversalRiskTable.js
//  SugLisSystem
//  万能险界面
//  Created by Mr.Right on 2013-05-06.
//  Copyright 2013 Mr.Right. All rights reserved.
//

var dB = require('untils/DB');
var exeSQL = require('untils/ExeSQL');
var UniversalDes = require('suggest/UniversalDesiger');

var TABLE_WIDTH = 580;
var TABLE_HEIGHT = 660;

var COLOR_NAME = '#FFFFFF';
var COLOR_PROP = '#FDF8E2';
//控件大小
var COMMON_HEIGHT = 42;
var COMMON_WIDTH = 580;
//字体大小
var FONT_SIZE = 16;
//基础控件位置
var WIDGET_TOP = 12;
//第一控件位置
var FIRST_WIDGET_LEFT = 17;
var FIRST_WIDGET_WIDTH = 370;
//第二控件位置
var SECORND_WIDGET_RIGHT = 43;
var SECORND_WIDGET_WIDTH = 200;

var isManual = false;

function UniversalRiskUI(riskCode, standByFlag3) {
	this.riskCode = riskCode;
	this.standByFlag3 = standByFlag3;
	var win = Ti.UI.createWindow({
		backgroundColor : '#FFF'
	});

	var ok_button = Ti.UI.createButton({
		title : '完 成',
		bottom : 10,
		width : '98%'
	});

	ok_button.addEventListener('click', function(e) {
		win.close();
	});

	win.add(ok_button);

	this.win = win;
}

UniversalRiskUI.prototype.create = function() {

	var riskTableUI = Ti.UI.createTableView({
		style : Ti.UI.iPhone.TableViewStyle.GROUPED,
		backgroundColor : 'transparent',
		width : TABLE_WIDTH,
		height : TABLE_HEIGHT,
	});
	// section数据
	var sectionData = [];

	//查看有多少类型的万能险界面
	var typeSQL = "Select CtrlType From LMUniversalRiskScreen Where RiskCode = '" + this.riskCode + "' Group By CtrlType";
	var typeArry = exeSQL.execSQL(typeSQL, exeSQL.TYPE_ARRAY);
	//开始生成界面
	for (var i = 0; i < typeArry.length; i++) {
		var universalType = typeArry[i][0];
		//查找类型标记
		var LMUniversalRiskScreenDB = new dB('LMUniversalRiskScreen');
		LMUniversalRiskScreenDB['riskcode'] = this.riskCode;
		LMUniversalRiskScreenDB['ctrltype'] = universalType;
		var LMUniversalRiskScreenSet = LMUniversalRiskScreenDB.query();
		var markObject = {};
		for (var m = 0; m < LMUniversalRiskScreenSet.length; m++) {
			var tempSchema = LMUniversalRiskScreenSet[m];
			markObject[tempSchema['ctrlcode']] = tempSchema['ctrlname'];
		}
		sectionData.push(createSection(riskTableUI, sectionData.length, markObject, universalType, this.standByFlag3));
	}

	riskTableUI.data = sectionData
	this.win.add(riskTableUI);
}

UniversalRiskUI.prototype.open = function() {
	this.win.open();
}

module.exports = UniversalRiskUI;

//创建万能险名称列
function createSection(riskTableUI, index, markObject, type, standByFlag3) {

	var section = Ti.UI.createTableViewSection({
		headerTitle : markObject['table']
	});

	var UniversalNameRow = Ti.UI.createTableViewRow({
		backgroundColor : COLOR_NAME,
		height : COMMON_HEIGHT,
		width : COMMON_WIDTH,
		selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE,
	});

	var riskNameLabel = Ti.UI.createLabel({
		text : markObject['table'],
		textAlign : 'left',
		font : {
			fontSize : FONT_SIZE,
			fontWeight : 'bold'
		},
		height : 'auto',
		width : FIRST_WIDGET_WIDTH,
		left : FIRST_WIDGET_LEFT,
		top : WIDGET_TOP
	});
	UniversalNameRow.add(riskNameLabel);

	var hiddenDetailView = Ti.UI.createImageView({
		image : 'images/detail_show.png',
		height : 33,
		width : 37,
		right : 10,
	});
	hiddenDetailView.isShow = true;
	hiddenDetailView.addEventListener('click', function(e) {
		hiddenDetailView.isShow = !hiddenDetailView.isShow;
		hiddenDetailView.image = hiddenDetailView.isShow ? 'images/detail_show.png' : 'images/detail_set.png'

		var totalLength = 0;
		for (var i = 0; i < index; i++) {
			totalLength += riskTableUI.data[i].rowCount;
		}

		if (hiddenDetailView.isShow) {
			var rows = createInfoRow(markObject, type, standByFlag3);
			for (var i = 0; i < rows.length; i++) {
				riskTableUI.insertRowAfter(totalLength, rows[i]);
			}
		} else {
			//TODO 存在问题
			isManual = true;
			for (var i = riskTableUI.data[index].rowCount - 1; i > 0; i--) {
				riskTableUI.deleteRow(i);
			}
			isManual = false;
		}
	});
	UniversalNameRow.add(hiddenDetailView);

	var addDetailView = Ti.UI.createImageView({
		image : 'images/detail_add.png',
		height : 33,
		width : 37,
		right : 50,
	});

	addDetailView.addEventListener('click', function(e) {
		//TODO 增加万能险数据
		var mUniversalDes = new UniversalDes(markObject, standByFlag3, type, function(dataId) {
			var totalLength = 0;
			for (var i = 0; i < index; i++) {
				totalLength += riskTableUI.data[i].rowCount;
			}

			if (hiddenDetailView.isShow) {
				var rows = createInfoRow(markObject, type, standByFlag3, dataId);
				for (var i = 0; i < rows.length; i++) {
					riskTableUI.insertRowAfter(totalLength, rows[i]);
				}
			}

		});
		mUniversalDes.create();
		mUniversalDes.open();
	});

	UniversalNameRow.add(addDetailView);

	section.add(UniversalNameRow);

	Ti.API.warn()

	var infoRows = createInfoRow(markObject, type, standByFlag3)
	for (var i = infoRows.length - 1; i >= 0; i--) {
		section.add(infoRows[i]);
	}
	return section;
}

//创建万能险具体信息列
function createInfoRow(markObject, type, standByFlag3, dataId) {
	var rows = []
	//查出所有数据
	var sql = '';
	if (dataId == null) {
		sql = "Select * From InvestRiskData Where SignNo = '" + standByFlag3 + "' And Type = '" + type + "' Order By Age DESC;";
	} else {
		sql = "Select * From InvestRiskData Where DataId = '" + dataId + "'";
	}
	var InvestRiskDataDB = new dB('InvestRiskData');
	var InvestRiskDataSet = InvestRiskDataDB.executeQuery(sql);
	for (var i = 0; i < InvestRiskDataSet.length; i++) {
		var InvestRiskDataSchema = InvestRiskDataSet[i];
		var dataStr = '';
		dataStr += (markObject['age'] + ': ' + InvestRiskDataSchema['age'] + '  ');
		dataStr += (markObject['degree'] + ': ' + InvestRiskDataSchema['degree'] + '  ');
		dataStr += (markObject['value'] + ': ' + InvestRiskDataSchema['value'] + '  ');

		var dataRow = Ti.UI.createTableViewRow({
			backgroundColor : COLOR_PROP,
			height : COMMON_HEIGHT,
			width : COMMON_WIDTH,
			editable : true,
			selectionStyle : Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE,
		});

		dataRow.id = InvestRiskDataSchema['dataid'];

		dataRow.addEventListener('delete', function(e) {
			if (!isManual) {
				var tempInvestRiskDataDB = new dB('InvestRiskData');
				tempInvestRiskDataDB['dataid'] = e.source.id;
				tempInvestRiskDataDB.remove();
			}
		});
		var dataLabel = Ti.UI.createLabel({
			text : dataStr,
			textAlign : 'left',
			font : {
				fontSize : FONT_SIZE,
				fontWeight : 'bold'
			},
			height : 'auto',
			width : 'auto',
			left : FIRST_WIDGET_LEFT,
			top : WIDGET_TOP
		});
		dataRow.add(dataLabel);
		rows.push(dataRow);
	}
	return rows;
}
